package com.cuijq.ms.singleton.generator;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Classname IdGenerator5
 * @Description 线程唯一,进程间可以不唯一
 * @Date 2021/5/5 9:52
 * @Created by CuiJQ
 */
public class IdGenerator5 {

  private AtomicLong id = new AtomicLong(0);
  private static final ConcurrentHashMap<Long, IdGenerator5> instances = new ConcurrentHashMap<>();

  private IdGenerator5() {
  }

  public static IdGenerator5 getInstance() {
    Long currentThreadId = Thread.currentThread().getId();
    instances.putIfAbsent(currentThreadId, new IdGenerator5());
    return instances.get(currentThreadId);
  }

  public long getId() {
    return id.incrementAndGet();
  }
}
